package com.zyt.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.zyt.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "9021000140632789";

    // 商户私钥，您的PKCS8格式RSA2私钥
    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCLuSne7HwFZDxtie05DBQK0/Z0w9VMKOyUTzX9J1kWq9X5bm2dpuTosgp54oYaSqQSy1ZNsNNQTS8sv99eAgBnsKuh+0VzNoCWWCpiSg7NpTp6ZcT2wqZWcjzcq6TsWLoYy1mejCVubcDq2P1ACVqTAsPBBPXeL1toy4vqUL4fLpYZi7CTeBVQa93QSM7Ozfj3YbaNU4wgi7zgmwiW4jZ69bfFNhQPiEbDTVeXkku8nLu7hBVgb6KPVooOTM5savV2p0TQe1AUeBoLakr2sH8dE41e7vEloSJpkjWiwErBWJlqtRJ0XFIYZwm33o+tY/UbkdIwjUc80iqTrvfWpp/zAgMBAAECggEAQGV1u82ol1+MTjv8fodnkVoBw+0iGHjgGLatPmDtOXq3Ir/Xe4qZA/pSw7w4bFOsbREUVLhDnEYGkQxZqV0ozKFraTuC1cfv2dsGpchWLEeTvSe8cLJjsYHzNQ1S9K8sDhNi2aTj95JUEKkzh1kISqtCh2p+2wleit0NPlA6X+oJQToOo1E21aG6DMrhohT8R12M1h65VgQ5Qk9nbX9/p4Lp08g/HATDhhk5++sazTRzYHobpfi6c5v/M5aon3IpOOq8duI7KO2eJsy/l9yFmUJ7qEtnwApj6R5dFR+FdpXfn9f5MFlEPEiq7j156is1nyyJnrvlId0VdHXLGuZMKQKBgQDrEAfq7TUOzJIfWL2j+AcyFtH8gCve+FASkIgiQIfNbTyivbBMWbvRRvPA4JBVP/qiwG2Gct4zWoKdlE/KyjOyfr59c6SBL6T0xfMcylwUaSCkPsUvSq0zx9TKQr5mTZOF5E9lp/Uy8BOnoLITJ3reFn2H3ajq26S+X1rTXyoArwKBgQCYKy0r4HSN9BpLpWVuYUBHA5mqrgWrTaFeMvJZD7g7YSZyMZo3G9otEICb3eAL7VH5enGxg/g81YbD39JY3rtwRNL1tZ8mVViofiIO8/nJBxePHlt5QnHrbqgAFszSpPvVi/GJO50HZXFZtYiUOS52hqzvMZz5OkyB/6KJB7n9/QKBgQCgk8j04aqXO4SuyU5zjuJLg29bkKtnnDYHGJkG3RJocTE082RWJA67/j5SCpZOxnAPcmjfqeTFLBWCVGjV3Z/HVfIHfVJptgR3+ioEBvE0OETGLuNBVUBQ2TgyG7o55NDcbIUYqs5ER+EFaC9+CkBTKwYCJJhaKEW4jydase3dnQKBgCrGCD6LK3gzo6ib53UtlRnhIzLdE+7hNXj7LTBnwMvANURoOQdApwV/3Ab0SWRQ43gTOR5DEGZZRzaMFd6xTVLzpbE3ynLGDtPeEMjPP+mIOtEaIXBmgwtA4ayUvCKCCeAvJEgFH9DSbRhgdPHlWPAsTzQ1aACOqK7qItAOAfQpAoGBAIO6Kv6PaZDLcrT1iLHo1nZBfBjQTP1e84jIeNvb7viK17X13Pwxu/qFzlgiYejQAl7/7IiVPRVKh3py2I0KJG1pGXJwuVgpGlG0TeiQNDZ+xK2eVJzTUtLM/KSAPGMXOsayZ5RWnUV9GbYK9Z3ULCu9rVtiV2Nr8FO/qwrXqOq4";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn3cm8GLmASrPLz5eJNHWmujXnE3Ubw32o9P0f6kSzVJQHIZvf97YNNCU1yaCISpoG0MHRnWlV0HE0X3NB/7iST8ju3K15+CP+CfIC3Bk214D9GnIjiuO4+euCoIvNkh+MdFDoAXyB5xtrk4lj+hDzyqu/8NuBHtlxIMR42lUs3j8p0AVf+QyfAZN2qzJ9c63ejH5Dkq111DAUqtyrxuEVWU8vKV9ujSSg8mvXdYrK44ebp97w73tx6esFLMN+HCjjsHobX36RVCIDRRyEGrMrP9F50eSXlOu25RSeWN21HYFQxyoY8yeiB8G5OA+OXVrHfOqllp601J22myPIMpZYwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url= "https://member.sumail.com/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url= "http://member.sumail.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
